package util;

import java.net.URLEncoder;

public class ViewUtils {
    public static final String defaultEncoding = "UTF-8";
    
    public static final long HUNDRED_MILLION = 100000000L;
    public static final long TEN_THOUSAND = 10000L;
    protected static ViewUtils singleton;

    // hide the constructor
    protected ViewUtils() {
    }
    
    static {
        singleton = new ViewUtils();
    }
    
    public static ViewUtils getInstance() {
        return singleton;
    }
    
    public enum CharType{
        DELIMITER,
        NUM,
        LETTER, // latin-1 letters and fullwidth letters
        OTHER,
        CHINESE;
    }
    
    /**
     * Get the type of the character
     */
    public static CharType getCharType(char c) {
        CharType ct =null;

        //Chinese, 0x4e00-0x9fbb
        if ((c >= 0x4e00)&&(c <= 0x9fbb)){
            ct = CharType.CHINESE;
        }

        //Halfwidth and Fullwidth Forms 0xff00-0xffef
        else if ( (c >= 0xff00) &&(c <= 0xffef)) {
            // 2 byte letters
            if ((( c >= 0xff21 )&&( c <= 0xff3a)) ||
                    (( c >= 0xff41 )&&( c <= 0xff5a))){
                ct = CharType.LETTER;
            }

            //2 byte digits
            else if (( c >= 0xff10 )&&( c <= 0xff19)  ){
                ct = CharType.NUM;
            }

            else ct = CharType.DELIMITER;
        }

        //basic latin， 0000-007f
        else if ( (c >= 0x0021) &&(c <= 0x007e)){
            //1 byte digit
            if (( c >= 0x0030 )&&( c <= 0x0039)  ){
                ct = CharType.NUM;
            }
            //1 byte letter
            else if ((( c >= 0x0041 )&&( c <= 0x005a)) ||
                    (( c >= 0x0061 )&&( c <= 0x007a)))        {
                ct = CharType.LETTER;
            }
            else ct = CharType.DELIMITER;
        }

        //latin-1 0080-00ff
        else if ( (c >= 0x00a1) &&(c <= 0x00ff)){
            if (( c >= 0x00c0 )&&( c <= 0x00ff)){
                ct = CharType.LETTER;
            }
            else ct = CharType.DELIMITER;
        }
        else ct = CharType.OTHER;
        return ct;
    }
    
    /**
     * Encode URL using defaultEncoding
     * @param s
     * @return
     */
    public static String encodeURL(String s) {
        return encodeURL(s, defaultEncoding);
    }
    /**
     * Encode URL using given charset
     * @param s
     * @param charset
     * @return the encoded URL in the format "%xx%xx..". If failed, return ""
     */
    public static String encodeURL(String s, String charset) {
        try {
            return URLEncoder.encode(s, charset);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * Convert special characters into HTML format.
     * will return "" if input is null
     */
    public static String getHTMLValidText(String s) {
        if(s == null) {
            return "";
        }
        StringBuilder buffer = new StringBuilder();
        int len = s.length();
        for(int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            if(ch == '"') {
                buffer.append("&quot;");
            } else if(ch == '&') {
                buffer.append("&amp;");
            } else if(ch == '<') {
                buffer.append("&lt;");
            } else if(ch == '>') {
                buffer.append("&gt;");
            } else if(ch == '\'') {
                buffer.append("&#39;");
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }
    public static String[] getHTMLValidText(String[] s) {
        String[] result = new String[s.length];
        for(int i = 0; i < result.length; i++) {
            result[i] = getHTMLValidText(s[i]);
        }
        return result;
    }
    public static String revertHTMLText(String s){
        return s.replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&quot;","\"").replaceAll("&amp;","&");
    }

    public static String truncateURL(String url, int length) {
        String result = url;
        if(url.startsWith("http://")) {
            result = url.substring("http://".length());
        }
        if(result.length() <= length) {
            return result;
        }
        return result.substring(0, length - 4) + " ...";
    }

    public static <T> String printArray(T[] array) {
        StringBuilder buffer = new StringBuilder();
        for(T t : array) {
            if(buffer.length() > 0) {
                 buffer.append(" ,");
            } else {
                 buffer.append("{");
            }
            buffer.append(t.toString());
        }
        buffer.append("}");
        return buffer.toString();
    }

}
